/*
Copyright 2010 Sieric
http://sieric.ca

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package savior.fs;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

public class Local implements FileSystem {

    private String startPath;

    public Local(String _startPath) {
        startPath = _startPath;

        // Add a delimiter if there are none at the end
        if (startPath.length() == 0) {
            startPath = "/";
        }
        if (!startPath.endsWith("/")) {
            startPath += '/';
        }
    }

    @Override
    public boolean createDirectory(String _path) {
        return (new File(startPath + _path)).mkdirs();
    }

    @Override
    public boolean deleteDirectory(String _path) {
        return deleteFile(_path);
    }

    @Override
    public boolean deleteFile(String _path) {
        return new File(startPath + _path).delete();
    }

    @Override
    public boolean directoryExists(String _path) {
        return (new File(startPath + _path)).exists();
    }

    @Override
    public InputStream getFile(String _path, long _offset) {
        FileInputStream result = null;

        try {
            result = new FileInputStream(startPath + _path);
            result.skip(_offset);
        } catch (Exception e) {
            result = null;
        }

        return (result != null) ? result : null;
    }

    @Override
    public List<String> listDirectories(String _path) {
        String[] fullList = (new File(startPath + _path)).list();
        List<String> result = new ArrayList<String>();

        for (int i = 0; i < fullList.length; i++) {
            if ((new File(startPath + _path + fullList[i]).isDirectory())) {
                result.add(fullList[i] + "/");
            }
        }

        return result;
    }

    @Override
    public List<FileDescription> listFiles(String _path) {
        String[] fullList = (new File(startPath + _path)).list();
        List<FileDescription> result = new ArrayList<FileDescription>();

        for (int i = 0; i < fullList.length; i++) {
            File file = new File(startPath + _path + fullList[i]);
            if (file.isFile()) {
                FileDescription fd = new FileDescription();
                fd.name = fullList[i];
                fd.size = file.length();
                fd.time = file.lastModified();
                result.add(fd);
            }
        }

        return result;
    }

    @Override
    public OutputStream putFile(String _path, boolean _append) {
        FileOutputStream result = null;
        try {
            result = new FileOutputStream(startPath + _path, _append);
        } catch (Exception e) {
            result = null;
        }

        return (result != null) ? result : null;
    }

    @Override
    public boolean setFileTime(String _path, long _time) {
        return (new File(startPath + _path)).setLastModified(_time);
    }

    @Override
    public boolean testConnection() {
        return (new File(startPath)).exists();
    }
}
